import QtQuick
import QtQuick.Controls
import QtQuick.Dialogs
import QtQuick.Controls.Material
import QtMultimedia

Item {
    id: titleBar

    Material.theme: Material.Dark
    Material.foreground: Material.Grey

    property Window mainWindow
    property MediaPlayer mediaPlayer
    property VideoOutput videoOutput
    readonly property string homePage: "https://gitee.com/leqijishu/leqiyingyin"

    height: frame.height

    signal fullScreen
    signal minimizeWindow
    signal closeWindow

    function loadUrl(url) {
        mediaPlayer.source = url
        if (mediaPlayer.hasAudio || mediaPlayer.hasVideo) {
            mediaPlayer.play()
        } else {
            if (mediaPlayer.errorString === ""){
                textError.text = qsTr("无效媒体文件，请重新选择文件")
            } else {
                textError.text = qsTr(mediaPlayer.errorString)
            }

            errorTips.open()
        }
    }

    Popup {
        id: errorTips
        x: (mainWindow.width - width) / 2
        y: (mainWindow.height - height)  / 2
        width: 200
        height: 68
        padding: 10
        modal: true
        focus: true
        contentItem: Text {
            id: textError
            anchors.centerIn: parent
            color: "white"
        }

        closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutsideParent
    }

    FileDialog {
        id: fileDialog
        title: qsTr("选择文件")
        onAccepted: {
            loadUrl(fileDialog.currentFile)
            videoName.text = fileDialog.currentFile
        }
    }

    Rectangle {
        id: frame
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.bottom: parent.top

        LeqiButton {
            id: leqiButton
            defaultIcon: "/resources/logo/logo32.png"
            hoverIcon: ""
            text: qsTr("乐其影音")
            Material.foreground: Material.Purple
            onClicked: {
                menu.open()
            }
        }

        Menu {
            id: menu
            x: 8
            y: leqiButton.height - 8
            width: 150
            MenuItem {
                text: qsTr("打开文件…")
                onTriggered: fileDialog.open()
            }
            Menu {
                title: qsTr("音轨")
            }
            Menu {
                title: qsTr("帮助")
                MenuItem {
                    text: qsTr("主页")
                    onTriggered: Qt.openUrlExternally(homePage)
                }
                MenuSeparator {}
                MenuItem {
                    text: qsTr("关于")
                }
            }
            MenuItem {
                text: qsTr("退出")
                onTriggered: closeWindow()
            }
        }

        Label {
            id: videoName
            anchors.centerIn: parent
        }

        LeqiButton {
            id: fullScreenButton
            defaultIcon: "/resources/images/fullscreen-8a.svg"
            hoverIcon: "/resources/images/fullscreen.svg"
            anchors.right: minimizeButton.left
            onClicked: fullScreen()
        }

        LeqiButton {
            id: minimizeButton
            defaultIcon: "/resources/images/minimize-8a.svg"
            hoverIcon: "/resources/images/minimize.svg"
            anchors.right: closeButton.left
            onClicked: minimizeWindow()
        }

        LeqiButton {
            id: closeButton
            defaultIcon: "/resources/images/close-8a.svg"
            hoverIcon: "/resources/images/close.svg"
            anchors.right: parent.right
            onClicked: closeWindow()
        }
    }
}
